
#pragma once

#include "..\Controls\ImageHelpers.h"
#include "..\Controls\vistalayer.h"
#include "FadeStateButton.h"

class CCatchScreenDlg;

class CCatchScreenPanel : 
   public ATL::CWindowImpl<CCatchScreenPanel>,
   public CVistaDwm
{
public:
   DECLARE_WND_CLASS(_T("CCatchScreenPanel"))

   enum { CX_PANELSIDE = 64 };

   
	CCatchScreenDlg* m_dlg;
   WTL::CBitmap m_bmpPanelLeft;
   WTL::CBitmap m_bmpPanelRight;
   WTL::CBitmap m_bmpPanelMiddle;
   WTL::CBitmap m_bmpPanelSide;
	

   CFadeStateButtonCtrl m_ctrlCopy;
   CFadeStateButtonCtrl m_ctrlEdit;
   CFadeStateButtonCtrl m_ctrlSave;
   CFadeStateButtonCtrl m_ctrlExit;


   BEGIN_MSG_MAP(CCatchScreenPanel)
      MESSAGE_HANDLER(WM_CREATE, OnCreate)
      MESSAGE_HANDLER(WM_PAINT, OnPaint)
      MESSAGE_HANDLER(WM_PRINTCLIENT, OnPaint)
      MESSAGE_HANDLER(WM_SIZE, OnSize)
      MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBkgnd)

      COMMAND_HANDLER(IDC_CATCH_SCREEN_COPY, BN_CLICKED, OnBitmapCopy)
      COMMAND_HANDLER(IDC_CATCH_SCREEN_EDIT, BN_CLICKED, OnBitmapEdit)
      COMMAND_HANDLER(IDC_CATCH_SCREEN_SAVE, BN_CLICKED, OnBitmapSave)      
	  COMMAND_HANDLER(IDC_CATCH_SCREEN_EXIT, BN_CLICKED, OnBitmapExit)      
   END_MSG_MAP()

   LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
   {
      m_bmpPanelLeft   = AtlLoadGdiplusImage(IDB_NAVPANEL_LEFT, _T("PNG"));
      m_bmpPanelRight  = AtlLoadGdiplusImage(IDB_NAVPANEL_RIGHT, _T("PNG"));
      m_bmpPanelSide   = AtlLoadGdiplusImage(IDB_NAVPANEL_SIDE, _T("PNG"));
      m_bmpPanelMiddle = AtlLoadGdiplusImage(IDB_NAVPANEL_MIDDLE, _T("PNG"));

      struct {
         CFadeStateButtonCtrl* pCtrl;
         UINT uID;
         DWORD dwStyle;
         UINT uBitmapID;
         UINT uToolTipText;
      } Buttons[] = 
      {
         { &m_ctrlCopy,     IDC_CATCH_SCREEN_COPY,     FSTBS_COMPOSITE | FSTBS_CAPTUREBKG | FSTBS_DOUBLEBUFFER, IDB_CATCHSCREEN_COPY,     IDS_CATCH_SCREEN_COPY },
         { &m_ctrlEdit,		IDC_CATCH_SCREEN_EDIT,  FSTBS_COMPOSITE | FSTBS_CAPTUREBKG | FSTBS_DOUBLEBUFFER, IDB_CATCHSCREEN_EDIT,  IDS_CATCH_SCREEN_EDIT },
         { &m_ctrlSave,     IDC_CATCH_SCREEN_SAVE,        FSTBS_COMPOSITE | FSTBS_CAPTUREBKG | FSTBS_DOUBLEBUFFER, IDB_CATCHSCREEN_SAVE,      IDS_CATCH_SCREEN_SAVE },
         { &m_ctrlExit,     IDC_CATCH_SCREEN_EXIT,   FSTBS_COMPOSITE | FSTBS_CAPTUREBKG | FSTBS_DOUBLEBUFFER, IDB_CATCHSCREEN_EXIT,   IDS_CATCH_SCREEN_EXIT },

      };
      for( int i = 0; i < sizeof(Buttons) / sizeof(Buttons[0]); i++ ) {
         CFadeStateButtonCtrl* pCtrl = Buttons[i].pCtrl;
         pCtrl->Create(m_hWnd, rcDefault, _T(""), WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_OWNERDRAW, 0, Buttons[i].uID);
		 pCtrl->SetToolTipText(BkString::Get(Buttons[i].uToolTipText));
         pCtrl->SetFadeButtonStyle(Buttons[i].dwStyle);
         pCtrl->SetBitmap(Buttons[i].uBitmapID, _T("PNG"));
         pCtrl->SizeToBitmap();
      }

      bHandled = FALSE;
      return 0;
   }

   LRESULT OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& bHandled)
   {
      CRect rcClient(0,0,LOWORD(lParam), HIWORD(lParam));
      CRect rcPanel = _GetPanelRect(rcClient);
    //  CPoint ptMid = rcPanel.CenterPoint();
      HDWP hdwp = ::BeginDeferWindowPos(4);
      hdwp = ::DeferWindowPos(hdwp, m_ctrlCopy,        NULL, rcPanel.left +4 , rcPanel.top + 4, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE);
      hdwp = ::DeferWindowPos(hdwp, m_ctrlEdit,       NULL, rcPanel.left + 36, rcPanel.top + 4, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE);
      hdwp = ::DeferWindowPos(hdwp, m_ctrlSave,        NULL, rcPanel.left + 68, rcPanel.top + 4, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE);
      hdwp = ::DeferWindowPos(hdwp, m_ctrlExit,         NULL, rcPanel.left + 100, rcPanel.top + 4, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE);

      ::EndDeferWindowPos(hdwp);
      bHandled = FALSE;
      return 0;
   }

   LRESULT OnEraseBkgnd(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
   {
      return 1;
   }

   LRESULT OnPaint(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
   {
      if( wParam != NULL )
      {
         DoPaint((HDC) wParam);
      }
      else
      {
         CPaintDC dc(m_hWnd);
         DoPaint(dc.m_hDC);
      }
      return 0;
   }

   LRESULT OnBitmapCopy(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);

   LRESULT OnBitmapEdit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
   LRESULT OnBitmapSave(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);

   LRESULT OnBitmapExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
  

   // Implementation

   CRect _GetPanelRect(CRect rcClient) const
   {
      if( rcClient.IsRectEmpty() ) return rcClient;
      rcClient.top = rcClient.top ;
      rcClient.bottom = rcClient.top + 32;
      CPoint ptMid = rcClient.CenterPoint();
      return CRect(ptMid.x - CX_PANELSIDE, rcClient.top, ptMid.x + CX_PANELSIDE, rcClient.bottom);
   }

   void DoPaint(CDCHandle dc)
   {    
      CRect rcClient;
      GetClientRect(&rcClient);

      COLORREF clrBack = RGB(120,167,224);
      //if( IsCompositionEnabled() ) clrBack = RGB(0,0,0);
      dc.FillSolidRect(rcClient, clrBack);

      CRect rc = rcClient;//_GetPanelRect(rcClient);
      CPoint ptMid = rc.CenterPoint();

      CRect rcLeft(rc.left, rc.top, rc.left + 16, rc.bottom);
      CRect rcRight(rc.right - 16, rc.top, rc.right, rc.bottom);
      CRect rcSide(rc.left + 16, rc.top, rc.right - 16, rc.bottom);
     // CRect rcMiddle(ptMid.x - 20, ptMid.y - 16, ptMid.x + 20, ptMid.y + 16);

      BLENDFUNCTION bf = { 0 };
      bf.BlendOp = AC_SRC_OVER; 
      bf.BlendFlags = 0; 
      bf.AlphaFormat = AC_SRC_ALPHA;
      bf.SourceConstantAlpha = 255;

      CDC dcCompat;
      dcCompat.CreateCompatibleDC(dc);
      HBITMAP hOldBitmap = dcCompat.SelectBitmap(m_bmpPanelLeft);
      dc.AlphaBlend(rcLeft.left, rcLeft.top, rcLeft.Width(), rcLeft.Height(), dcCompat, 0, 0, rcLeft.Width(), rcLeft.Height(), bf);
      dcCompat.SelectBitmap(m_bmpPanelRight);
      dc.AlphaBlend(rcRight.left, rcRight.top, rcRight.Width(), rcRight.Height(), dcCompat, 0, 0, rcRight.Width(), rcRight.Height(), bf);
      dcCompat.SelectBitmap(m_bmpPanelSide);
      dc.AlphaBlend(rcSide.left, rcSide.top, rcSide.Width(), rcSide.Height(), dcCompat, 0, 0, 5, rcSide.Height(), bf);
      //dcCompat.SelectBitmap(m_bmpPanelMiddle);
     // dc.AlphaBlend(rcMiddle.left, rcMiddle.top, rcMiddle.Width(), rcMiddle.Height(), dcCompat, 0, 0, rcMiddle.Width(), rcMiddle.Height(), bf);
      dcCompat.SelectBitmap(hOldBitmap);
   }
};


